/*! jMenu.js
 * ================
 * AdminLTE Menu Creator
 * dependence on AdminLTE 2.1.2
 *
 * @Author  zengzhijun
 * @Support <http://www.almsaeedstudio.com>
 * @Email   <zzjvslove@qq.com>
 * @version 1.0.0
 * @license MIT <http://opensource.org/licenses/MIT>
 */
var TgfMenu = (function ($, AdminLTE) {

    var NON_HREF = "javascript:void(0);"
    var BASE_PATH = "/pages";

    var defOpts = {
        url:'/',
        type:'post',
        dataType: 'json',
        traditional: false,
        successCode: 200,
        timeout: 30000
    };
    //entrance
    function treeMenu(options) {
        var options = $.extend(defOpts, options);

        $.ajax({
            url: options.url,
            type: options.type || 'post',
            dataType: options.dataType || 'json',
            data: options.where || {},
            success:function(res) {
                var menu = generatedMenu(res.data);
                $('.sidebar-menu').append(menu);
                //$.AdminLTE.tree('.sidebar');
                tree('.sidebar');
            }
        });

        // $('.sidebar-menu').on('click','a',function () {
        //     //console.log($(this).attr('href'))
        //
        //     $('.content-wrapper .content iframe').attr('src', BASE_PATH + $(this).attr('href'));
        //     return false;
        // });

    };

    function tree(menu) {
        var _this = this;
        var animationSpeed = $.AdminLTE.options.animationSpeed;
        $("li a", $(menu)).on('click', function (e) {
            //Get the clicked link and the next element
            var $this = $(this);
            var ulEle = $this.next();

            //下一个元素是 下拉列表，并且是 展开 状态
            if ((ulEle.is('.treeview-menu')) && (ulEle.is(':visible'))) {
                // 关闭当前 以及 子集 列表
                ulEle.slideUp(animationSpeed, function () {
                    ulEle.removeClass('menu-open');
                    //Fix the layout in case the sidebar stretches over the height of the window
                    //_this.layout.fix();
                });
                ulEle.parent("li").removeClass("active");

                var childrens = ulEle.find('.treeview-menu:visible');
                if(childrens.length > 0) {
                    for (var i = 0; i < childrens.length; i++) {
                        var children = $(childrens[i]);
                        children.slideUp(animationSpeed, function () {
                            children.removeClass('menu-open');
                            //Fix the layout in case the sidebar stretches over the height of the window
                            //_this.layout.fix();
                        });
                        children.parent("li").removeClass("active");
                    }
                }
            }
            else if ((ulEle.is('.treeview-menu')) && (!ulEle.is(':visible'))) {
                //先把同级的 ul 收缩
                var sibActLi = ulEle.parent('li').siblings(".active");

                var childrens = sibActLi.find('.treeview-menu:visible');
                if(childrens.length > 0) {
                    for (var i = 0; i < childrens.length; i++) {
                        var children = $(childrens[i]);
                        children.slideUp(animationSpeed, function () {
                            children.removeClass('menu-open');
                            //Fix the layout in case the sidebar stretches over the height of the window
                            //_this.layout.fix();
                        });
                        children.parent("li").removeClass("active");
                    }
                }

                ulEle.slideDown(animationSpeed, function () {
                    //Add the class active to the parent li
                    ulEle.addClass('menu-open');
                    //parent.find('li.active').removeClass('active');
                    ulEle.parent('li').addClass('active');
                    //Fix the layout in case the sidebar stretches over the height of the window
                    $.AdminLTE.layout.fix();
                });
            }
            //if this isn't a link, prevent the page from being redirected
            if (ulEle.is('.treeview-menu')) {
                e.preventDefault();
            }
        });
    }
    
    // create menu
    function generatedMenu(list) {
        var menu = '';
        for(var i = 0; i < list.length; i++) {
            var d = list[i];
            menu += liHtml(d);
        }
        return menu;
    }
    // create li
    function liHtml(obj) {
        var html = '';
        if(obj.children == undefined || obj.children.length <= 0){
            html += liItem(obj);
        }else{
            html += liItemNested(obj);
        }
        return html;
    }
    // create li item
    function liItemNested(obj) {
        var html = '';
        html += '<li class="treeview">';
        html += '  <a href="'+ hrefItemValue(obj.href) +'">';
        html += iconItem(obj);
        html += '       <span>'+ obj.name +'</span>';
        html += '       <i class="fa fa-angle-left pull-right"></i>';
        html += '   </a>';
        html += '  <ul class="treeview-menu">';
        //每一个子集
        var oc = obj.children;
        for(var i = 0; i < oc.length; i++) {
            html += liHtml(oc[i]);
        }
        html += '  </ul>';
        html += '</li>';
        return html;
    }

    function liItem(obj) {
        var li_item = '';
        li_item += '<li><a href="'+ hrefItemValue(obj.href) +'">';
        li_item += iconItem(obj);
        li_item += obj.name;
        li_item += '</a>';
        li_item += '</li>';
        return li_item;
    }
    // create icon
    function iconItem(obj) {
        if(obj.icon == null || obj.icon == '') {
            return '<i class=""></i> ';
        }
        return '<i class="'+ obj.icon +'"></i> ';
    }
    function hrefItemValue(href) {
        if(href == null || href=='') {
            return NON_HREF;
        }

        else if(!isURL(href)) {
            return NON_HREF;
        }
        else{
            return href;
        }
    }
    function isURL(href) {
        return /^(\/\w+)+.(htm|html)$/.test(href)
            || /((http|https):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\%\!\&=\+\~\:\#\;\,]*)?)/ig.test(href);
    }

    return {
        menu: treeMenu
    };
})(jQuery, $.AdminLTE);